package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.FpFaUserAddressDTO;
import com.financing.wap.service.IFpFaUserAddressService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpFaUserAddressController")
@RequestMapping("/api/fpFaUserAddress")
public class FpFaUserAddressController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaUserAddressService fpFaUserAddressService;
	
	@RequestMapping("/listFpFaUserAddress")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserAddressDTO fpFaUserAddress,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserAddress list begin .......");
			
			fpFaUserAddress.setBegin(pager.getStartIndex());
			fpFaUserAddress.setPageSize(pager.getPageSize());
			fpFaUserAddress.setUserId(getSession(true).getUser().getId());
			
			int count = fpFaUserAddressService.queryListCount(fpFaUserAddress);
			
			pager.setTotalCount(count);
			
			List<FpFaUserAddressDTO> fpFaUserAddressList = null;
			
			if(count > 0){
				fpFaUserAddressList = fpFaUserAddressService.queryList(fpFaUserAddress);
			}else{
				fpFaUserAddressList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaUserAddressList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserAddress list end .......");
		}catch(Exception e){
			log.error("query fpFaUserAddress error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaUserAddress")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserAddressDTO fpFaUserAddress){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUserAddress  begin .......");


			
			DTOdateSetUtil.setCreateInfo(fpFaUserAddress);
			DTOdateSetUtil.setUpdateInfo(fpFaUserAddress);

			fpFaUserAddress.setUserId(getSession(true).getUser().getId());
			fpFaUserAddress.setDefaultad(0);// 非默认
			int id = fpFaUserAddressService.insert(fpFaUserAddress);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUserAddress list end .......");
		}catch(Exception e){
			log.error("add fpFaUserAddress error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaUserAddress")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaUserAddressDTO fpFaUserAddress){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaUserAddress  begin .......");
			
			Integer id = fpFaUserAddress.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserAddressDTO dto = fpFaUserAddressService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaUserAddress  end .......");
		}catch(Exception e){
			log.error("query detail fpFaUserAddress error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaUserAddress")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserAddressDTO fpFaUserAddress){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUserAddress  begin .......");
			
			Integer id = fpFaUserAddress.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpFaUserAddressService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaUserAddress  end .......");
		}catch(Exception e){
			log.error("delete fpFaUserAddress error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaUserAddress")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaUserAddressDTO fpFaUserAddress){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaUserAddress  begin .......");
			
			Integer id = fpFaUserAddress.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpFaUserAddress);
			fpFaUserAddress.setUserId(getSession(true).getUser().getId());
			fpFaUserAddressService.update(fpFaUserAddress);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaUserAddress list end .......");
		}catch(Exception e){
			log.error("update fpFaUserAddress error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaUserAddress")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaUserAddressDTO fpFaUserAddress){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaUserAddress  begin .......");
			
			Integer id = fpFaUserAddress.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpFaUserAddress);
			fpFaUserAddress.setUserId(getSession(true).getUser().getId());
			fpFaUserAddressService.updateForce(fpFaUserAddress);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaUserAddress list end .......");
		}catch(Exception e){
			log.error("updateForce fpFaUserAddress error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
